package com.zjw.zy.coreui.fragment.memorandum

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.base.BaseRecyclerAdapter
import com.zjw.zy.base.GestureRecyclerView
import com.zjw.zy.bean.MemorandumItem
import com.zjw.zy.constant.ImageSubPath
import com.zjw.zy.coreui.fragment.memorandum.MemorandumTextFragment.Companion.TEXT_DATA_KEY
import com.zjw.zy.coreui.fragment.adapter.MemorandumAdapter
import com.zjw.zy.coreui.fragment.holder.MemorandumHolder
import com.zjw.zy.coreui.view.SearchView
import com.zjw.zy.ext.*
import com.zjw.zy.utils.*
import com.zjw.zy.viewmodel.MemorandumViewModel

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/9/30 8:48
 * @description：备忘录主页
 */
class MemorandumMainFragment : BaseFragment() {

    private val mMemorandumViewModel: MemorandumViewModel by viewModels()

    private val mList: GestureRecyclerView by lazy { findViewById(R.id.memorandum_list) }
    private val mSearch: SearchView by lazy { findViewById(R.id.memorandum_search_bar) }

    private val mAdapter: MemorandumAdapter =
        MemorandumAdapter(object : BaseRecyclerAdapter.ItemListener<MemorandumHolder> {
            override fun onItemClick(holder: MemorandumHolder, v: View) {
            }

            override fun onItemLongClick(holder: MemorandumHolder, v: View): Boolean {
                return false
            }

            override fun onItemDoubleClick(holder: MemorandumHolder, v: View) {

            }

            override fun onCallbackOtherEvent(event: String, vararg args: Any?) {
                if (event == MemorandumHolder.DELETE_EVENT) {
                    mMemorandumViewModel.deleteMemorandumData(args[0] as MemorandumItem)
                    mMemorandumViewModel.requestMemorandumData()
                }
                if (event == MemorandumHolder.EDIT_EVENT) {
                    WindowUtils.FootBar.hide()
                    goWithTopAnim(
                        R.id.nav_memorandum_text_fragment,
                        Bundle().apply {
                            putParcelable(TEXT_DATA_KEY, args[0] as MemorandumItem)
                        })
                }
            }
        })

    override fun onPageEnter() {
        WindowUtils.FootBar.show()
        WindowUtils.TopBar.showTopBarNav()
        WindowUtils.TopBar.hideAllRightButton()
        WindowUtils.TopBar.show1RightButton(ResourcesUtils.getDrawable(R.drawable.icon_forum_add)) {
            goWithTopAnim(
                R.id.nav_memorandum_text_fragment
            )
        }
        WindowUtils.TopBar.show2RightButton(ResourcesUtils.getDrawable(R.drawable.icon_cramer_image)) {
            goImageContent(PathUtils.getPrivateImagePath(ImageSubPath.MEMORANDUM_IMAGE_PATH))
        }
        WindowUtils.TopBar.setTopBarLabelText(ResourcesUtils.getString(R.string.memorandum_topbar_label))
        mList.focus()
        mMemorandumViewModel.requestMemorandumData()
        mainActivity().setFullBackGround(R.color.memorandum_list_color)

    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {
        mMemorandumViewModel.getData().observe(this) {
            mAdapter.notifyDataWithDiff(it)
        }
    }

    override fun onBindViewListener() {
        mSearch.bindListener(object : SearchView.OnSearchListener {
            override fun onSearchTextChange(keyword: String) {
                mMemorandumViewModel.queryByKeyWord(keyword)
            }
        })
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onBindViewValue() {
        mList.adapter = mAdapter
        mList.layoutManager = LinearLayoutManager(mainActivity())

        //获得焦点
        mList.setOnTouchListener { v, event ->
            mList.focus()
            return@setOnTouchListener false
        }
    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {

        return R.layout.fragment_memorandum_main
    }

    override fun onPageExit() {

    }

    override fun onBackClick() {
        super.onCallMoveToBack()
    }
}